﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Security;
using Groupware;

namespace TripLOA.GUI.secure
{
    public partial class MyFriends :TripLOA.GUI.internationalization.LOABasePage
    {
        
        TripLoaUser us;
        Profile userProfile,userFriendProfile;  
              
        string userIdentity;

        protected void Page_Load(object sender, EventArgs e)
        {
          
            userIdentity = User.Identity.Name;
            us = Security.TripLoaUserManager.Instance.getUser(userIdentity);
            userProfile = us.GetProfile();

            if (GridView1.Rows.Count < 1)
            {
                Button2.Visible = false;
                lbl_msg_noFriends.Visible = true;
            }
            else
            {
                Button2.Visible = true;
                lbl_msg_noFriends.Visible = false;
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(TextBox1.Text))
            {
                try
                {
                    Storage.EntityModels.User frienduser = Storage.StorageManager.Instance.GetUserByUserName(TextBox1.Text);
                    if (!frienduser.BookedIn) throw new Storage.Exceptions.UserNotFoundException(frienduser.Id);
                }
                catch (Storage.Exceptions.UserNotFoundException)
                {
                    Label3.Visible = true;
                    Label3.ForeColor = System.Drawing.Color.Red;
                    Label3.Text = GetLocalResourceObject("userNotFound").ToString() + TextBox1.Text;
                    TextBox1.Text = String.Empty;
                    return;
                }

                if (TextBox1.Text == us.UserName)
                {
                    Label3.Visible = true;
                    Label3.ForeColor = System.Drawing.Color.Red;
                    Label3.Text = GetLocalResourceObject("sameUser").ToString();
                    TextBox1.Text = String.Empty;
                    return;
                }

                userFriendProfile = us.GetProfile(TextBox1.Text);
                FriendshipRequest req = new FriendshipRequest();

                if (req.AskForFriendship(us, userFriendProfile.Id))
                {
                    Label3.Visible = true;
                    Label3.Text = GetLocalResourceObject("sendReqOK").ToString();
                    Label3.ForeColor = System.Drawing.ColorTranslator.FromHtml("#C9F912");
                    TextBox1.Text = String.Empty;
                }
                else
                {
                    Label3.Visible = true;
                    Label3.ForeColor = System.Drawing.Color.Red;
                    Label3.Text = GetLocalResourceObject("errorSend").ToString() + TextBox1.Text;
                    TextBox1.Text = String.Empty;
                    return;
                }

                LinqDataSource1.DataBind();
                GridView1.DataBind();

                if (GridView1.Rows.Count < 1)
                {
                    Button2.Visible = false;
                    lbl_msg_noFriends.Visible = true;
                }
                else
                {
                    Button2.Visible = true;
                    lbl_msg_noFriends.Visible = false;
                }
            }
            else
            {
                Label3.Visible = true;
                Label3.ForeColor = System.Drawing.Color.Red;
                Label3.Text = GetLocalResourceObject("notValidName").ToString();
                TextBox1.Text = String.Empty;
            }

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            int i;
            for (i=0; i < GridView1.Rows.Count; i++)
            {
                GridViewRow row = GridView1.Rows[i];
                bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;
                GridView1.SelectedIndex = i;

                if (isChecked)
                {
                    DataKey user = GridView1.SelectedDataKey;
                    userFriendProfile = us.GetProfile(user.Value.ToString());
                    us.RemoveFriend(userFriendProfile.Id);                  
                    
                }
            }

            Label3.Visible = false;

            LinqDataSource1.DataBind();
            GridView1.DataBind();

            if (GridView1.Rows.Count < 1)
            {
                Button2.Visible = false;
                lbl_msg_noFriends.Visible = true;
            }
            else
            {
                Button2.Visible = true;
                lbl_msg_noFriends.Visible = false;
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            int i;
            for (i = 0; i < GridView1.Rows.Count; i++)
            {
                GridViewRow row = GridView1.Rows[i];
                bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;
                GridView1.SelectedIndex = i;
          
                if (isChecked)
                {
                    DataKey user = GridView1.SelectedDataKey;
                    
                    userFriendProfile = us.GetProfile(user.Value.ToString());
                    us.BanUser(userFriendProfile.Id);
                   
                }
            }

            Label3.Visible = false;

            LinqDataSource1.DataBind();
            GridView1.DataBind();

            LinqDataSource2.DataBind();
            GridView2.DataBind();

        }

       

       
    }
}
